package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.CategoryDiagram;
import com.itheima.reggie.dto.DishDiagram;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : xyiaao
 * @createDate : 2022/1/28 14:49
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 删除分类,并判断分类下是否关联菜品与套餐
     * @param id
     */
    @Override
    public void remove(Long id) {

        //条件查询,判断分类下是否关联菜品
        LambdaQueryWrapper<Dish> queryWrapperDish =new LambdaQueryWrapper<>();
        queryWrapperDish.eq(Dish::getCategoryId,id);
        int countDish = dishService.count(queryWrapperDish);
        if(countDish > 0){
            throw  new CustomException("当前分类下关联菜品,不能删除");
        }

        //条件查询,判断分类下是否关联套餐
        LambdaQueryWrapper<Setmeal> queryWrapperSetmeal = new LambdaQueryWrapper<>();
        queryWrapperSetmeal.eq(Setmeal::getCategoryId,id);
        int countSetmeal = setmealService.count(queryWrapperSetmeal);
        if (countSetmeal > 0 ){
            throw new CustomException("当前分类下关联套餐,不能删除");
        }

        //正常删除
        super.removeById(id);
    }
}
